﻿Imports System.Data.Sql
Imports System.ComponentModel

<ToolboxItem(False)>
Public Class ucSQLInstall
    Private mSQLWinAuth As Boolean
    Private mSQLUName As String
    Private mSQLPWord As String
    Private Sub ucSQLInstall_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DisplayMessage("")
        DisplayProgress("")
        btnAction.Enabled = False

        'mSQLUName = StartupSettings.SQLServerUserName
        'mSQLPWord = StartupSettings.SQLServerPassword

        'If StartupSettings.SQLServerName <> "" Then cmbServer.Text = StartupSettings.SQLServerName

        'If StartupSettings.SQLServerWindowsAuthentication = True Then
        '    cmbAuth.Text = "Windows Authentication"
        'Else
        '    cmbAuth.Text = "SQL Server Authentication"
        'End If

        If MyApp.IsUpdate = True Then
            cmbServer.Enabled = False
            cmbAuth.Enabled = False
            txtUName.Enabled = False
            txtPWord.Enabled = False
        End If
    End Sub
    Private Sub cmbServer_DropDown(ByVal sender As Object, ByVal e As EventArgs) Handles cmbServer.DropDown
        Cursor.Current = Cursors.WaitCursor
        Dim oTable As New DataTable
        Try
            If cmbServer.Items.Count = 0 Then
                oTable = SqlDataSourceEnumerator.Instance.GetDataSources
                For Each oRow As DataRow In oTable.Rows
                    If oRow("InstanceName").ToString = "" Then
                        cmbServer.Items.Add(oRow("ServerName"))
                    Else
                        cmbServer.Items.Add(String.Format("{0}\{1}", oRow("ServerName"), oRow("InstanceName")))
                    End If
                Next oRow
            End If
        Catch ex As Exception
            DisplayMessage(ex.Message)
        Finally
            If oTable IsNot Nothing Then
                oTable.Dispose()
            End If
        End Try
        Cursor.Current = Cursors.Default
    End Sub
    Sub DisplayProgress(Message As String)
        lblProgress.Text = Message
        Application.DoEvents()
    End Sub
    Sub DisplayMessage(Message As String)
        lblMessage.Text = Message
        Application.DoEvents()
    End Sub
    Private Sub cmbAuth_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbAuth.SelectedIndexChanged
        If cmbAuth.Text = "Windows Authentication" Then
            mSQLWinAuth = True
            txtUName.Text = ""
            txtPWord.Text = ""
            txtUName.Enabled = False
            txtPWord.Enabled = False
        Else
            mSQLWinAuth = False
            txtUName.Text = mSQLUName
            txtPWord.Text = mSQLPWord
            txtUName.Enabled = True
            txtPWord.Enabled = True
        End If
    End Sub
    Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
        SetSQLStartupSettings()
    End Sub
    Sub SetSQLStartupSettings()
        DisplayMessage("")
        If cmbServer.Text.Trim = "" Then
            DisplayMessage("Please select a server")
            Exit Sub
        End If

        If mSQLWinAuth = False Then
            If txtUName.Text.Trim = "" Then
                DisplayMessage("Please select a valid user name")
                Exit Sub
            End If
            If txtPWord.Text.Trim = "" Then
                DisplayMessage("Please select a valid password")
                Exit Sub
            End If
        End If

        'StartupSettings.SQLServerName = cmbServer.Text.Trim
        'StartupSettings.SQLServerWindowsAuthentication = mSQLWinAuth
        'StartupSettings.SQLServerUserName = txtUName.Text.Trim
        'StartupSettings.SQLServerPassword = txtPWord.Text.Trim
        'StartupSettings.SaveFile()
    End Sub
    Private Sub cmbServer_TextChanged(sender As Object, e As EventArgs) Handles cmbServer.TextChanged
        If cmbServer.Text.Trim <> "" Then
            btnAction.Enabled = True
        Else
            btnAction.Enabled = False
        End If
    End Sub
End Class
